package top.mschen.blog.controller.feignInterfcace;

import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.mschen.blog.common.config.SpringMultipartEncoder;

import java.util.List;
import java.util.Map;

//，通过@ FeignClient（“服务名”），来指定调用哪个服务
@FeignClient(name = "feignprovider",configuration = DemoFegin.MultipartSupportConfig.class,fallback = DemoFegin.DefaultFallError.class)
public interface DemoFegin {

    @RequestMapping(value = "/hello")
    String sayHiFromClientOne(@RequestParam("name") String name);

    @GetMapping("/get001/{id}")
//    @RequestMapping(method= RequestMethod.GET, value = "/get001/{id}")
    public Domain getDomain001(@PathVariable("id") int id);

    @GetMapping("/get002/{id}")
    public Domain getDomain002(@PathVariable("id") int id);

    //多参数调用 可以是两种
    @GetMapping("/get003")
    public Domain getDomain003(@RequestParam("userName") String userName,@RequestParam("pwd")String pwd,@RequestParam("address")String address);

    @GetMapping("/get004")
    public Domain getDomain004(@RequestParam Map<String,Object> map);

    @PostMapping("/post001")
    public Domain postDomain001(@RequestBody Domain domain);

    @PostMapping("/post002")
    public Domain postDomain002(@RequestBody Domain domain);

    @PostMapping("/post003")
    public List<Domain> postDomain003(@RequestBody Domain domain);

    /**
     * 上传必须制定
     * consumes = MediaType.MULTIPART_FORM_DATA_VALUE
     * @param file
     * @return
     */
    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String handleFileUpload(@RequestPart("file") MultipartFile file,@RequestParam("title") String title);


    @PostMapping(value = "/uploadFileMutil", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String handleFileUploadMutil(@RequestPart("file") MultipartFile[] files,@RequestParam("title") String title);


    @Configuration
    class MultipartSupportConfig {
//        @Bean
//        public Encoder feignFormEncoder() {
//            return new SpringFormEncoder();
//        }


        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignEncoder() {
            return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
        }

    }

    @Component
    static  class DefaultFallError implements DemoFegin{
        @Override
        public String sayHiFromClientOne(String name) {
            return "请稍后在试试!!!!";
        }

        @Override
        public Domain getDomain001(int id) {
            return null;
        }

        @Override
        public Domain getDomain002(int id) {
            return null;
        }

        @Override
        public Domain getDomain003(String userName, String pwd, String address) {
            return null;
        }

        @Override
        public Domain getDomain004(Map<String, Object> map) {
            return null;
        }

        @Override
        public Domain postDomain001(Domain domain) {
            return null;
        }

        @Override
        public Domain postDomain002(Domain domain) {
            return null;
        }

        @Override
        public List<Domain> postDomain003(Domain domain) {
            return null;
        }

        @Override
        public String handleFileUpload(MultipartFile file, String title) {
            return null;
        }

        @Override
        public String handleFileUploadMutil(MultipartFile[] files, String title) {
            return null;
        }
    }
}
